package com.yjy.moudle_pair;

import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.yjy.mybase.fragment.MVPBaseFragment;
import com.yjy.mybase.http.ICallback;
import com.yjy.mybase.http.SuperHttpHelper;
import com.yjy.mybase.model.BaseBean;
import com.yjy.mybase.mvp.MVPBasePresenter;
import com.yjy.mybase.utils.ARouterUri;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;

/**
 * author : YangJinyang
 * create : 2019-10-29 18:16
 * description :
 */
public class PairFragment extends MVPBaseFragment {


    RecyclerView pairRv;
    SwipeRefreshLayout pairRefresh;

    private int page = 1;
    private PairAdapter mAdapter;

    @Override
    protected MVPBasePresenter createPresenter() {
        return null;
    }

    @Override
    protected void onViewCreated() {
        pairRv = mRootView.findViewById(R.id.pair_rv);
        pairRefresh = mRootView.findViewById(R.id.pair_refresh);
        pairRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getData(true);
            }
        });
        mAdapter = new PairAdapter();
        pairRv.setLayoutManager(new LinearLayoutManager(getContext()));
        pairRv.setAdapter(mAdapter);
        mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                getData(false);
            }
        });
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                ARouter.getInstance().build(ARouterUri.ACTIVITY_NEWSDETAIL).withString("id", mAdapter.getData().get(position).getId()).navigation();
            }
        });
        getData(true);
    }

    private void getData(boolean isRefresh) {
        if (isRefresh) {
            page = 1;
        } else {
            page++;
        }

        Map<String, String> pram = new HashMap<>();
        pram.put("pageNo", "" + page);
        SuperHttpHelper.superHttpHelper().get(SuperHttpHelper.NewsList, pram, new ICallback<BaseBean<NewsBean>>() {


            @Override
            public void onSuccess(BaseBean<NewsBean> data) {
                pairRefresh.setRefreshing(false);
                if (page == 1) {
                    mAdapter.setNewData(data.getResult().getRecords());
                } else {
                    mAdapter.addData(data.getResult().getRecords());
                    mAdapter.loadMoreComplete();
                }

            }

            @Override
            public void onFail(int errCode, String errMsg) {
                toast(errMsg);
                pairRefresh.setRefreshing(false);

            }
        });

    }

    @Override
    public void lazyLoadData() {
        super.lazyLoadData();
        pairRefresh.setRefreshing(true);


    }

    //首次修改方便提交
    @Override
    protected int layout() {
        return R.layout.fragment_pair;
    }
}
